package ac.manchester.cs.afzal.tcm_processing.terms_sentence_handler;

import ac.manchester.cs.afzal.tcm_processing.miscellaneous.File_Manager_Impl;
import ac.manchester.cs.afzal.tcm_processing.sql_handler.SQL_Handler_Sentences_Impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Vector;

public class Distinct_Sentence_Mapping_Impl
{
    private Properties props;
    private String database_url = "database_url";
    private String username = "username";
    private String password = "password";

    private Vector<String> sen_values;
    private Vector<String> doc_ids;
    private Vector<String> d_doc_ids;
    private Vector<String> d_sen_ids;
    
    private String DISTINCT_SENTENCES_DIR_PATH = "DISTINCT_SENTENCES_DIR_PATH";

    private File_Manager_Impl fileManager;
    private SQL_Handler_Sentences_Impl sql_handler;

    public Distinct_Sentence_Mapping_Impl()
    {
        try
        {
            props = new Properties();
            props.load(new FileInputStream("Term_Classification.properties"));
            database_url = props.getProperty(database_url);
            username = props.getProperty(username);
            password = props.getProperty(password);
            DISTINCT_SENTENCES_DIR_PATH = props.getProperty(DISTINCT_SENTENCES_DIR_PATH);            
        }
        catch (IOException ex)
        {
           System.out.println("Value of exception is : " + ex.getMessage());
        }
    }


 public void populate_distinct_sentences()
    {
        sql_handler = new SQL_Handler_Sentences_Impl();
        sql_handler.initialize_parameters(database_url, username, password);
        sql_handler.connect_db();

        String[] tables =
        {
            "Distinct_Sentences"
        };
        sql_handler.reset_tables(tables);

        doc_ids = sql_handler.get_all_documents_ids_from_Sentences();

        for(String doc_id: doc_ids)
        {
            sen_values = sql_handler.get_distinct_sentences_per_doc(doc_id);

            for(String sen_value:sen_values)
            {
                String[] doc_table =
                {
                    "Doc_ID", "Value"
                };
                String[] doc_table_value =
                {
                    doc_id.toString(), sen_value
                };
                sql_handler.insert_table("Distinct_Sentences", doc_table, doc_table_value);
            }
        }

    }

     /*
     * It populates the sentence_mapping table. It maps the sentences from Sentences 
     * table to the distinct sentences in Distinct_Sentences table and put the mapping
     * into Distinct_Sentence_Mapping_Impl table.
     */
    public void sentence_mapping()
    {
        sql_handler = new SQL_Handler_Sentences_Impl();
        sql_handler.initialize_parameters(database_url, username, password);
        sql_handler.connect_db();

        String[] tables = {"Sentence_Mapping"};
        sql_handler.reset_tables(tables);


        Vector<String> doc_ids = sql_handler.get_all_documents_ids_from_Sentences();
        
        for (String doc_id : doc_ids)
        {
            Vector<String> sen_values = sql_handler.get_sentence_id_and_value_per_doc(doc_id);
            Vector<String> sen_ids = sql_handler.get_sen_ids();

            for (int j = 0; j < sen_values.size();j++)
            {
                String sentence_value = sen_values.elementAt(j).toString();
                String sentence_id = sen_ids.elementAt(j).toString();
                int distinct_sentence_id = sql_handler.get_distinct_sen_id_per_sentence_value_and_doc_id(sentence_value, doc_id);

                String[] doc_table = {"Sen_ID", "D_Sen_ID"};
                String[] doc_table_value = {sentence_id,Integer.toString(distinct_sentence_id)};
                sql_handler.insert_table("Sentence_Mapping", doc_table, doc_table_value);

            }
        }
    }
 
    /*
     * It downloads all sentences from Distinct_Sentences Table, and stores them in local hard disk
     */
    public void download_distinct_sentences()
    {
       fileManager = new File_Manager_Impl();
        sql_handler = new SQL_Handler_Sentences_Impl();
        sql_handler.initialize_parameters(database_url, username, password);
        sql_handler.connect_db();
        d_doc_ids = sql_handler.get_all_documents_ids_from_distinct_Sentences();

        for(String doc_id:d_doc_ids)
        {
            File doc_folder = new File(DISTINCT_SENTENCES_DIR_PATH + doc_id);
            doc_folder.mkdir();

            sen_values = sql_handler.get_all_distinct_sentences_per_doc(doc_id);
            d_sen_ids = sql_handler.get_d_sen_ids();

            for(int i = 0; i < sen_values.size(); i ++)
            {
                String file_id = d_sen_ids.elementAt(i).toString();
                String sentence_value = sen_values.elementAt(i).toString();
                fileManager.fileWriter(sentence_value, DISTINCT_SENTENCES_DIR_PATH + doc_id + "//" + file_id + ".txt");
            }
        }
        sql_handler.close_db();
    }
}